package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.web.bean.SmsMgrTemplateInfo;
import com.nl.controller.web.bean.querySent.CountInfo;
import com.nl.controller.web.bean.querySent.CountInfoLess;
import com.nl.controller.web.mapper.SmsMgrTemplateInfoMapper;
import com.nl.controller.web.mapper.SmsQuerySentMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SmsQuerySentService {
	@Autowired
	private SmsQuerySentMapper smsQuerySentMapper;
	@Autowired
	private SmsMgrTemplateInfoMapper smsMgrTemplateInfoMapper;
	@SystemServiceLog(description = "气象局发送量查询接口", value = false)
	public JSONObject querySentInfo(String taskId){
		Map<String,String> map = new HashMap<String, String>();
		JSONObject json = new JSONObject();
		String mgrId = smsQuerySentMapper.findMgrIdByTaskId(taskId);
		if (null==mgrId || "".equals(mgrId)) {
			json.put("resultCode", "-1");
			json.put("resultDesc", "失败");
			return json;
		}
		log.info("通过taskId"+taskId+"查询项目id:"+mgrId);
		SmsMgrTemplateInfo smsMgrTemplateInfo = smsMgrTemplateInfoMapper.findMgrTemplateByMgrId(mgrId);
		String isOver = "0".equals(smsMgrTemplateInfo.getMgrState()) || "1".equals(smsMgrTemplateInfo.getMgrState())   ? "1" : "0";
		String regionType = smsMgrTemplateInfo.getRegionType();
		
		if("1".equals(regionType)){
			//List<CountInfoLess> countyListLess = new ArrayList<CountInfoLess>();
			List<CountInfoLess> cityListLess = new ArrayList<CountInfoLess>();
			List<CountInfoLess> provinclistLess = new ArrayList<CountInfoLess>();
			//countyListLess = smsQuerySentMapper.querySentCountyLess(mgrId);
			cityListLess = smsQuerySentMapper.querySentCityLess(mgrId);
			provinclistLess = smsQuerySentMapper.querySentProvinceLess(mgrId);
			if (null != provinclistLess && !provinclistLess.isEmpty()) {
				for (CountInfoLess countInfoLess : provinclistLess) {
					countInfoLess.setSendScope("25");
				}
			}
			List<CountInfoLess> list = new ArrayList<CountInfoLess>();
			list.addAll(provinclistLess);
			list.addAll(cityListLess);    
			//list.addAll(countyListLess);
			list = ITIdToQAJIdLess(list);
			
			if (null == list || list.isEmpty()) {
				list = new ArrayList<CountInfoLess>();
				CountInfoLess sentInfo = new CountInfoLess();
				sentInfo.setSendScope("320000000000");
				sentInfo.setSendCount("0");
				//sentInfo.setSuccessCount("0");
				list.add(sentInfo);
			}
			json.put("countInfo", list);
		}else{
			//List<CountInfo> countyList = new ArrayList<CountInfo>();
			List<CountInfo> cityList = new ArrayList<CountInfo>();
			List<CountInfo> provinclist = new ArrayList<CountInfo>();
			//countyList = smsQuerySentMapper.querySentCounty(mgrId);
			cityList = smsQuerySentMapper.querySentCity(mgrId);
			for(CountInfo countInfo: cityList){
				String cityId = countInfo.getSendScope();
				map.put("mgrId",mgrId);
				map.put("cityId",cityId);
				String successCount=smsQuerySentMapper.querySuccessCount(map);
				countInfo.setSuccessCount(successCount);
			}

			List<CountInfo> list = new ArrayList<CountInfo>();

			if("5".equals(regionType)){
				log.info("非省气象局项目无需封装全省发送量参数");
			}else{
				provinclist = smsQuerySentMapper.querySentProvince(mgrId);
				if (null != provinclist && !provinclist.isEmpty()) {
					for (CountInfo countInfo : provinclist) {
						countInfo.setSendScope("25");
					}
				}
				list.addAll(provinclist);
			}


			list.addAll(cityList);
			//list.addAll(countyList);
			list = ITIdToQAJId(list);
			
			if (null == list || list.isEmpty()) {
				
				list = new ArrayList<CountInfo>();
				CountInfo sentInfo = new CountInfo();
				sentInfo.setSendScope("320000000000");
				sentInfo.setSendCount("0");
				sentInfo.setSuccessCount("0");
				list.add(sentInfo);
			}
			json.put("countInfo", list);
			}
		json.put("resultCode", "0");
		json.put("resultDesc", "成功");
		json.put("isOver", isOver);
		json.put("taskId", taskId);
		return json;
	}

	private List<CountInfo> ITIdToQAJId(List<CountInfo> list){
		if (null !=list && ! list.isEmpty()) {
			for (CountInfo sentInfo : list) {
				String id = sentInfo.getSendScope();
				id = smsQuerySentMapper.findQXJIdByid(id);
				sentInfo.setSendScope(id);;
			}
			return list;
		}else {
			return null;
		}

	}
	
	private List<CountInfoLess> ITIdToQAJIdLess(List<CountInfoLess> list){
		if (null !=list && ! list.isEmpty()) {
			for (CountInfoLess sentInfo : list) {
				String id = sentInfo.getSendScope();
				id = smsQuerySentMapper.findQXJIdByid(id);
				sentInfo.setSendScope(id);;
			}
			return list;
		}else {
			return null;
		}

	}
}
